using System.Net.Http;
using System.Web.Http;
using AutoMapper;
using Common.Views;
using Dto.ApiRequests.TrackLogForms;
using Dto.ApiResponses;
using Dto.ApiResponses.TrackLogResponses;
using NServiceBus;
using PetaPoco;
using Sanelib.Common.Helpers;
using Sanelib.DataView;
using WebApp.Services;
using Common.Commands.TrackLogCommands;
using System;

namespace WebApp.ControllerApis
{
    public class TrackLogController : SmartApiController
    {
        private readonly IViewRepository<TrackLogView> _trackLogsRepository;
        public IBus Bus { get; set; }
        public TrackLogController(IUserSession userSession, IMappingEngine mappingEngine, IViewRepository<TrackLogView> trackLogsRepository)
            : base(userSession, mappingEngine)
        {
            _trackLogsRepository = trackLogsRepository;
        }

         [ActionName("BySpecification")]
        public HttpResponseMessage Get([FromUri]SearchSpecification specification)
        {
            var trackLogsViewPage = _trackLogsRepository.SearchBySpecification(specification, Property.Of<TrackLogView>(x => x.DetectedOn));
            var response = Mapper.Map<Page<TrackLogView>, PageResponse<TrackLogLine>>(trackLogsViewPage);

            return Content(response);
        }

        public HttpResponseMessage Post(AddTrackLogForm form)
        {
            var command = Mapper.Map<AddTrackLogForm, AddTrackLog>(form);
            return ExecuteCommand(command);
        }

        public HttpResponseMessage Put(UpdateTrackLogForm form)
        {
            var command = Mapper.Map<UpdateTrackLogForm, UpdateTrackLog>(form);
            return ExecuteCommand(command);
        }

        public HttpResponseMessage Delete(Guid id)
        {
            var command = new DeleteTrackLog(id);
            return ExecuteCommand(command);
        }
    }
}